<script>
import Play from "@/page/play/index.vue";
import SearchHtml from "@/page/search/index.vue";
import { ref, onMounted, getCurrentInstance, reactive } from "vue";
//引入路由
import { useRouter, useRoute } from "vue-router";
import { useStore } from "vuex";
export default {
  name: "index",
  components: { Play,SearchHtml },
  setup() {
    const store = useStore();
    //创建route
    const router = useRouter();
    const { proxy } = getCurrentInstance();
  
    const SongNum = ref(0);
    const obj = reactive({
      bannerlist: [],
      RecommendSongsList: "",
      RecommendedText: "推荐歌单",
      customizedText: "为你定制精选歌曲",
      customizedList: "",
      swipewidth: 350,
      RankingListText: "排行榜",
      RankingListList: [],
    });
    const getIndex = () => {
      proxy.$http
        .get(proxy.$api+"/homepage/block/page")
        .then((response) => {
          let videodata = response.data;
          if (typeof videodata == "string") {
            videodata = JSON.parse(videodata);
          }
          const data = videodata.data;
          console.log(data.blocks);
          // 轮播图
          data.blocks[0].extInfo.banners.map((i, k) => {
            obj.bannerlist.push(i);
          });
          // 推荐歌单
          obj.RecommendedText = data.blocks[1].uiElement.subTitle.title;
          obj.RecommendSongsList = data.blocks[1].creatives;
          console.log(obj.RecommendSongsList,'obj.RecommendSongsList-------')
          // 为你定制精选歌曲
          obj.customizedText = data.blocks[2].uiElement.subTitle.title;
          obj.customizedList = data.blocks[2].creatives;
          // 排行榜
          obj.RankingListText = data.blocks[3].uiElement.subTitle.title;
          data.blocks[3].creatives.map((i, k) => {
            if (i.creativeType == "toplist") {
              obj.RankingListList.push(i);
            }
          });
          console.log(obj.RankingListList, "obj.RankingListList");
        });
    };
    //点击去专辑页
    const swipeClick = (id, type, songid) => {
      console.log(id, type, songid, "id, type, songid");
      if (type == "新碟首发") {
        router.push({
          path: "/album",
          query: {
            id: id,
          },
        });
      } else if (type == "歌单") {
        router.push({
          path: "/recommend",
          query: {
            id: id,
          },
        });
      } else if (songid != 0 && (type != "MV首发" || type != "热歌推荐")) {
        const data = {
          songid: songid,
          songnum: 1,
        };
        store.commit("songID", data);
      }
    };
    // 为你定制
    const customiZedClick = (id) => {
      const data = {
        songid: id,
        songnum: 1,
      };
      store.commit("songID", data);
    };
    // 推荐歌单
    const getRecommendSongs = () => {
      proxy.$http.get(proxy.$api+"/personalized").then((response) => {
        let data = response.data;
        if (typeof data == "string") {
          data = JSON.parse(data);
        }
        obj.RecommendSongsList = data.result;
      });
    };
    const recommendClick = (id) => {
      router.push({
        path: "/recommend",
        query: {
          id: id,
        },
      });
    };
    // 排行榜
    const RankIngClick = (id) => {
      const data = {
        songid: id,
        songnum: 1,
      };
      store.commit("songID", data);
    };
    /* 菜单 */
    const onMenuClick =()=>{
      console.log(12)
    };
    onMounted(() => {
      getIndex();
      getRecommendSongs();
    });
    return {
      obj,
      SongNum,
      swipeClick,
      recommendClick,
      customiZedClick,
      RankIngClick,
      onMenuClick
    };
  },
};
</script>

<template>
  <div class="app" ref="app">
    <van-sticky position="top" z-index="9">
      <div class="search-top" style="background: #fff">
        <van-icon name="wap-nav" size="24" color="#333" @click.stop="onMenuClick()"/>
        <SearchHtml></SearchHtml>
      </div>
    </van-sticky>
    <!-- swiper -->
    <van-swipe
      class="my-swipe"
      :autoplay="3000"
      indicator-color="white"
      lazy-render
    >
      <van-swipe-item
        v-for="(item, index) in obj.bannerlist"
        :key="index"
        @click="swipeClick(item.encodeId, item.typeTitle, item.targetId)"
      >
        <img class="bannerImg" :src="item.pic" />
      </van-swipe-item>
    </van-swipe>
    <!-- 推荐歌单 -->
    <div class="scrollTltle">{{ obj.RecommendedText }}</div>
    <div class="scroll-wrapper">
      <div class="scroll-content">
        <div
          class="item"
          v-for="(item, index) in obj.RecommendSongsList"
          :key="index"
          @click="recommendClick(item.creativeId)"
        >
          <van-swipe
            v-if="index == 0"
            :autoplay="5000"
            vertical
            :show-indicators="false"
          >
            <van-swipe-item
              v-for="(swipeitem, idx) in item.resources"
              :key="idx"
              @click="recommendClick(swipeitem.creativeId)"
            >
              <div class="img">
                <img v-lazy="swipeitem.uiElement.image.imageUrl" />
                <div class="play">
                  <van-icon name="play" color="#fff" size="16" />
                </div>
                <div class="playnum">
                  <van-icon name="play" color="#fff" size="12" />
                  <span
                    >{{
                      parseInt(swipeitem.resourceExtInfo.playCount / 10000)
                    }}万</span
                  >
                </div>
              </div>
              <div class="name">
                {{ item.uiElement.mainTitle.title }}
              </div>
            </van-swipe-item>
          </van-swipe>
          <div v-else>
            <div class="img">
              <img v-lazy="item.resources[0].uiElement.image.imageUrl" />
              <div class="play">
                <van-icon name="play" color="#fff" size="16" />
              </div>
              <div class="playnum">
                <van-icon name="play" color="#fff" size="12" />
                <span
                  >{{
                    parseInt(
                      item.resources[0].resourceExtInfo.playCount / 10000
                    )
                  }}万</span
                >
              </div>
            </div>
            <div class="name">
              {{ item.resources[0].uiElement.mainTitle.title }}
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- xian -->
    <div class="xian"></div>
    <!-- 为你定制精选歌曲 -->
    <div class="customiZedtle">{{ obj.customizedText }}</div>
    <div class="customiZed-swiper">
      <van-swipe :loop="false" :width="obj.swipewidth" :show-indicators="false">
        <van-swipe-item
          v-for="(item, index) in obj.customizedList"
          :key="index"
        >
          <div
            class="list"
            v-for="(listItem, idx) in item.resources"
            :key="idx"
            @click="customiZedClick(listItem.resourceExtInfo.song.id)"
          >
            <img class="img" v-lazy="listItem.uiElement.image.imageUrl" />
            <div class="mainTitle">
              <div class="songname van-ellipsis">
                {{ listItem.uiElement.mainTitle.title }}
              </div>
              <div class="subTitle van-ellipsis">
                <span
                  class="tapS"
                  v-if="listItem.uiElement.subTitle ? true : false"
                >
                  {{
                    listItem.uiElement.subTitle
                      ? listItem.uiElement.subTitle.title
                      : ""
                  }}
                </span>
                <span
                  class="tapname"
                  v-for="(spanitem, spanindex) in listItem.resourceExtInfo
                    .artists"
                  :key="spanindex"
                >
                  {{ spanitem.name }}
                </span>
              </div>
            </div>
          </div>
        </van-swipe-item>
      </van-swipe>
    </div>
    <!-- xian -->
    <div class="xian"></div>
    <!-- 排行榜 -->
    <div class="RankingListtle">
      {{ obj.RankingListText }}
    </div>
    <div class="RankingSwiper">
      <van-swipe :loop="false" :width="350" :show-indicators="false">
        <van-swipe-item
          v-for="(item, index) in obj.RankingListList"
          :key="index"
        >
          <div class="swipelist">
            <div class="title">
              <span class="titleS">{{ item.uiElement.mainTitle.title }}</span>
              <span class="titleD">{{
                item.uiElement.mainTitle.titleDesc
              }}</span>
            </div>
            <ul>
              <li
                v-for="(rankIngItem, idx) in item.resources"
                :key="idx"
                @click="RankIngClick(rankIngItem.resourceId)"
              >
                <img
                  class="img"
                  v-lazy="rankIngItem.uiElement.image.imageUrl"
                />
                <span :class="'num num_' + (idx + 1)">{{ idx + 1 }}</span>
                <div class="text">
                  <div class="name">
                    {{ rankIngItem.uiElement.mainTitle.title }}
                  </div>
                  <div
                    class="songsname"
                    v-for="(k, i) in rankIngItem.resourceExtInfo.artists"
                    :key="i"
                  >
                    {{ k.name }}
                  </div>
                </div>
                <span class="tapS">
                  {{ rankIngItem.uiElement.labelText.text }}
                </span>
              </li>
            </ul>
          </div>
        </van-swipe-item>
      </van-swipe>
    </div>
  </div>
</template>

<style lang="less" scoped>
.app {
  width: 98%;
  height: 100%;
  // overflow-y: scroll;
  margin: 0 auto;
  padding-bottom: 100px;
  background: #fff;
  .xian {
    background: #ebecf0;
    width: 100%;
    height: 1px;
    margin: 10px 0;
  }
  .search-top {
    display: flex;
    align-items: center;
    background: #fff;
  }
  .bannerImg {
    width: 100%;
  }
  .scrollTltle,
  .customiZedtle,
  .RankingListtle {
    font-size: 14px;
    font-weight: 600;
    margin: 10px 0;
  }
  .scroll-wrapper {
    width: 100%;
    overflow-y: scroll;
    position: relative;
    white-space: nowrap;
    &::-webkit-scrollbar {
      display: none;
    }
    .scroll-content {
      display: inline-block;
      .item {
        display: inline-block;
        width: 130px;
        height: 157px;
        margin-right: 15px;
        overflow: hidden;
        .img {
          width: 130px;
          height: 120px;
          border-radius: 10px;
          margin-right: 15px;
          overflow: hidden;
          position: relative;
          .play {
            position: absolute;
            bottom: 5px;
            right: 5px;
          }
          .playnum {
            position: absolute;
            top: 2px;
            right: 5px;
            span {
              font-size: 12px;
              font-weight: 600;
              color: #fff;
            }
          }
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }
        .name {
          font-size: 12px;
          color: #333;
          line-height: 16px;
          text-align: justify;
          margin-top: 5px;
          white-space: normal;
        }
      }
    }
  }
  .customiZed-swiper {
    width: 100%;
    .list {
      display: flex;
      margin: 8px 0;
      width: 100%;
      .img {
        width: 50px;
        height: 50px;
        border-radius: 5px;
        margin-right: 10px;
      }
      .mainTitle {
        flex: 1;
        width: 60%;
        .songname {
          width: 70%;
          font-size: 14px;
          color: #333;
          font-weight: 700;
        }
        .subTitle {
          width: 70%;
          .tapS {
            font-size: 12px;
            color: red;
            border-radius: 50px;
            transform: scale(0.8);
            display: inline-block;
            font-family: PingFangSC-Regular;
            background: #edc3c3;
            line-height: 18px;
            padding: 0 7px;
          }
          .tapname {
            font-size: 12px;
            color: #ccc;
            margin-right: 5px;
          }
        }
      }
    }
  }
  .RankingSwiper {
    width: 100%;
    .swipelist {
      box-shadow: 0px 0px 4px -2px #000;
      width: 90%;
      margin: 10px auto;
      height: 200px;
      border-radius: 5px;
      padding: 5px;
      .title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .titleS {
          font-size: 14px;
          color: #333;
          font-weight: 700;
        }
        .titleD {
          font-size: 12px;
          color: #ccc;
          letter-spacing: 1.5px;
        }
      }
      li {
        display: flex;
        align-items: center;
        margin: 5px 0;
        .img {
          width: 50px;
          height: 50px;
          border-radius: 5px;
          margin-right: 10px;
        }
        .num {
          margin-right: 10px;
        }
        .num_1 {
          color: rgb(255, 0, 0);
        }
        .num_2 {
          color: rgb(255, 81, 0);
        }
        .num_3 {
          color: rgb(255, 187, 0);
        }
        .tapS {
          font-size: 12px;
          color: rgb(255, 0, 0);
          margin-right: 10px;
        }
        .text {
          flex: 1;
          .name {
            font-size: 13px;
            font-weight: 700;
            color: "#333";
          }
          .songsname {
            font-size: 12px;
            color: #ccc;
            line-height: 16px;
          }
        }
      }
    }
  }
}
</style>

